package com.das.mechanic_base.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3ScreenUtils;

public class X3FirstAloneOilView extends View {

    private Context mContext;

    /**
     * 主画笔
     */
    private Paint mPaint;

    /**
     * 最大分隔块的数量
     */
    private int maxNum = 8;

    /**
     * 文字画笔
     */
    private Paint tPaint;

    /**
     * 距离顶部
     */
    private int topMargin;

    /**
     * 分隔块之间的距离
     */
    private int paddingMargin;

    /**
     * 分隔块的宽度
     */
    private int rectWidth,rectHeight;

    private int oilNum = -1;

    private Bitmap oilBitmap;

    private String[] str;

    public X3FirstAloneOilView(Context context) {
        this(context,null);
    }

    public X3FirstAloneOilView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public X3FirstAloneOilView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);

        tPaint = new Paint();
        tPaint.setStyle(Paint.Style.FILL);
        tPaint.setAntiAlias(true);
        tPaint.setTextSize(X3ScreenUtils.dipToPx(10,mContext));
        tPaint.setColor(ContextCompat.getColor(mContext,R.color.bg_707380));

        topMargin = X3ScreenUtils.dipToPx(15,mContext);
        paddingMargin = X3ScreenUtils.dipToPx(4,mContext);
        rectHeight = X3ScreenUtils.dipToPx(34,mContext);

        oilBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.first_alone_oil_icon, null);

        str = new String[]{"1/4","1/2","3/4",mContext.getString(R.string.x3_full_oil)};

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        rectWidth = (getWidth() - paddingMargin * 7) / 8;

        // 绘制背景
        drawBackGround(canvas);

        drawPrograss(canvas);

        // 绘制背景下方的文字提示
        drawBottomText(canvas);

        /**
         * 绘制分隔符
         */
        mPaint.setColor(Color.WHITE);
        for (int i = 0; i < maxNum - 1; i++) {
            int left = (i + 1) * rectWidth + i * paddingMargin;
            canvas.drawRect(new RectF(left,topMargin,left + paddingMargin,topMargin + rectHeight),mPaint);
        }
    }

    public void changePrograss(int oilNum) {
        if (oilNum < 0) {
            oilNum = 0;
        }
        this.oilNum = oilNum;
        invalidate();
    }

    public int getCurrentPrograss() {
        return oilNum;
    }

    private void drawPrograss(Canvas canvas) {
        if (oilNum == -1) {
            return;
        }
        mPaint.setColor(oilNum == 0 ? ContextCompat.getColor(mContext,R.color.bg_F64444) :ContextCompat.getColor(mContext,R.color.bg_0077FF));
        int right = (oilNum + 1) * rectWidth + oilNum * paddingMargin;
        if (right < rectWidth) {
            right = rectWidth;
        }
        canvas.drawRoundRect(new RectF(0,topMargin,right,topMargin + rectHeight),
                X3ScreenUtils.dipToPx(10,mContext),X3ScreenUtils.dipToPx(10,mContext),mPaint);

        if (oilNum == 7) {
            return;
        }
        // 补齐圆角的地方
        if (oilNum == 0) {
            canvas.drawRect(new RectF(oilNum * (rectWidth + paddingMargin) + rectWidth / 2,topMargin,oilNum * (rectWidth + paddingMargin) + rectWidth,topMargin + rectHeight),mPaint);
            return;
        }
        canvas.drawRect(new RectF(oilNum * (rectWidth + paddingMargin),topMargin,oilNum * (rectWidth + paddingMargin) + rectWidth,topMargin + rectHeight),mPaint);

    }

    private void drawBottomText(Canvas canvas) {
        canvas.drawBitmap(oilBitmap,(rectWidth - oilBitmap.getWidth()) / 2, (float) (topMargin + rectHeight + paddingMargin * 2 + paddingMargin / 2),mPaint);

        Rect rect = new Rect();
        tPaint.getTextBounds(str[0],0,str[0].length(),rect);
        canvas.drawText(str[0],rectWidth * 2 + paddingMargin + paddingMargin / 2 - rect.width() / 2,topMargin + rectHeight + paddingMargin * 3 + rect.height(),tPaint);

        tPaint.getTextBounds(str[1],0,str[1].length(),rect);
        canvas.drawText(str[1],rectWidth * 4 + paddingMargin * 3 + paddingMargin / 2 - rect.width() / 2,topMargin + rectHeight + paddingMargin * 3 + rect.height(),tPaint);

        tPaint.getTextBounds(str[2],0,str[2].length(),rect);
        canvas.drawText(str[2],rectWidth * 6 + paddingMargin * 5 + paddingMargin / 2 - rect.width() / 2,topMargin + rectHeight + paddingMargin * 3 + rect.height(),tPaint);

        tPaint.getTextBounds(str[3],0,str[3].length(),rect);
        canvas.drawText(str[3],getWidth() - rect.width() - paddingMargin * 2,topMargin + rectHeight + paddingMargin * 3 + rect.height(),tPaint);

    }

    /**
     * 绘制背景
     * @param canvas
     */
    private void drawBackGround(Canvas canvas) {
        mPaint.setColor(Color.parseColor("#F2F2F5"));
        canvas.drawRoundRect(new RectF(0,topMargin,getWidth(),topMargin + rectHeight),
                X3ScreenUtils.dipToPx(10,mContext),X3ScreenUtils.dipToPx(10,mContext),mPaint);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_UP:
                getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                getAfterOil(event.getX());
                break;
            case MotionEvent.ACTION_UP:
                getAfterOil(event.getX());
                break;
        }
        return true;
    }

    private void getAfterOil(float x) {
        if (x >= (rectWidth + paddingMargin) * 7) {
            if (oilNum == maxNum - 1) {
                return;
            }
            oilNum = maxNum - 1;
            invalidate();
            return;
        }

        if (oilNum == (int) (x / (rectWidth + paddingMargin))) {
            return;
        }

        oilNum = (int) (x / (rectWidth + paddingMargin));

        invalidate();
    }
}
